#region File Header
//----------------------------------------------------------------
// Copyright (C) 2002 Stefan Kyntchev
// All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY 
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT 
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR 
// FITNESS FOR A PARTICULAR PURPOSE.
//----------------------------------------------------------------
#endregion File Header

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
//using THUMBEXTRACTLib;
using AirWeb.ApplicationTools.Common;

namespace AirWeb.ApplicationTools.Image
{
	/// <summary>
	/// This class generates a thumbnail image given a new size.
	/// The image can be generated either by using the Image class or
	/// if the image is a movie, by using a COM component
	/// If the Bevel proprty is set to true the images will have a bevel
	/// around the edges
	/// </summary>
	public class ThumbNailGenerator
	{
		private string m_Original;
		private Bitmap m_Bitmap;
		//private IFileThumbExtract m_COMExtractor;
		private bool m_Bevel = true;

		bool Bevel {set{ m_Bevel = value;}	get{ return m_Bevel;}}

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="i_fileName">The original image</param>
		public ThumbNailGenerator(String i_fileName)
		{
			m_Original = i_fileName;
		}

		/// <summary>
		/// Generates a thumbnail image and saves it to tye specified path.Keeps the aspect ratio
		/// of the image
		/// </summary>
		/// <param name="i_size">the maximum size of the image in any direction</param>
		/// <param name="i_filename">path to be saved</param>
		public void Generate(int i_size, string i_filename)
		{
			if(!CommonHelper.IsMovie(m_Original))
				GenerateKnownType(i_size,i_filename);
			//else
			//  Use COM to extract a thumbnail directlu from the file
			//	GenerateUsingCOM(i_size, i_filename);
		}

		/// <summary>
		/// Generates an image using intenral Bitmap class. Keeps the aspect ratio
		/// of the image
		/// </summary>
		/// <param name="i_size">the maximum size of the image in any direction</param>
		/// <param name="i_filename">path to be saved</param>
		void GenerateKnownType(int i_size, string i_filename)
		{
			if(m_Bitmap == null)
				m_Bitmap = new Bitmap(m_Original);

			float neww,newh;
			if(m_Bitmap.Width >= m_Bitmap.Height)
			{
				neww = i_size;
				float ratio = (float)m_Bitmap.Width/(float)m_Bitmap.Height;
				newh = neww/ratio;
			}
			else
			{
				newh = i_size;
				float ratio = (float)m_Bitmap.Height/(float)m_Bitmap.Width;
				neww = newh/ratio;
			}

			// If the original is smaller than the generated just use the original
			if(m_Bitmap.Width < neww && m_Bitmap.Height < newh)
			{
				m_Bitmap.Save(i_filename,m_Bitmap.RawFormat);
			}
			else
			{
				Bitmap b = new Bitmap(m_Bitmap,(int)neww,(int)newh);
				ApplyBevel(b);
				b.Save(i_filename,m_Bitmap.RawFormat);
				b.Dispose();
			}

			m_Bitmap.Dispose();
		}

		/// <summary>
		/// Generates an image using a COM class
		/// </summary>
		/// <param name="i_size"></param>
		/// <param name="i_filename"></param>
		void GenerateUsingCOM(int i_size, string i_filename)
		{
			try
			{
				/*if(m_COMExtractor == null)
					m_COMExtractor = new FileThumbExtractClass();

				m_COMExtractor.SetPath(m_Original);
				m_COMExtractor.SetThumbnailSize(i_size,i_size);
				IntPtr hBitmap = (IntPtr)m_COMExtractor.ExtractThumbnail();
				Bitmap b = Bitmap.FromHbitmap(hBitmap);
				m_COMExtractor.FreeThumbnail();
				i_filename = System.IO.Path.ChangeExtension(i_filename,"jpg");

				ApplyBevel(b);
				b.Save(i_filename,ImageFormat.Jpeg);
				b.Dispose();*/
			}
			catch
			{}
		}

		/// <summary>
		/// Applies a bevel effect to the bitmap.
		/// This code is taken from an article contributed by
		/// Philipos Sakellaropoulos on "The Code project" web site
		/// </summary>
		private void ApplyBevel(Bitmap i_bitmap)
		{
			if(!Bevel)
				return;

			int widTh,heTh;

			widTh = i_bitmap.Width;
			heTh = i_bitmap.Height;

			int BevW = 10, LowA=0, HighA=180, Dark=80, Light=255;

			// hilight color, low and high
			Color clrHi1 = Color.FromArgb(LowA,Light,Light,Light);
			Color clrHi2 = Color.FromArgb(HighA,Light,Light,Light);
			Color clrDark1 = Color.FromArgb(LowA,Dark,Dark,Dark);
			Color clrDark2 = Color.FromArgb(HighA,Dark,Dark,Dark);

			LinearGradientBrush br; Rectangle rectSide;

			Graphics newG = Graphics.FromImage(i_bitmap);
   
			Size szHorz = new Size(widTh,BevW);
			Size szVert = new Size(BevW,heTh);

			// ---- draw dark (shadow) sides first
			// draw bottom-side of bevel
			szHorz += new Size(0,2); szVert+=new Size(2,0);
			
			rectSide = new Rectangle(new Point(0,heTh-BevW),szHorz);
			
			br = new LinearGradientBrush(rectSide,clrDark1,clrDark2,LinearGradientMode.Vertical);
			rectSide.Inflate(0,-1);
			newG.FillRectangle(br,rectSide);  
 
			// draw right-side of bevel
			rectSide = new Rectangle(new Point(widTh-BevW,0),szVert);
			br = new LinearGradientBrush(rectSide,clrDark1,clrDark2,LinearGradientMode.Horizontal);
			rectSide.Inflate(-1,0);
			newG.FillRectangle(br,rectSide);
  
			// ---- draw bright (hilight) sides next
			szHorz-=new Size(0,2); szVert-=new Size(2,0);

			// draw top-side of bevel
			rectSide = new Rectangle(new Point(0,0),szHorz);
			br = new LinearGradientBrush(rectSide,clrHi2,clrHi1,LinearGradientMode.Vertical);
			newG.FillRectangle(br,rectSide); 
 
			// draw left-side of bevel
			rectSide = new Rectangle(new Point(0,0),szVert);
			br = new LinearGradientBrush(rectSide,clrHi2,clrHi1,LinearGradientMode.Horizontal);
			newG.FillRectangle(br,rectSide); 

			// dispose graphics objects
			br.Dispose(); newG.Dispose();
		}
	}
}
